package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private MybatisPlusInterceptor mybatis;

    @GetMapping("/hi")
    public String hi() {
        return "hi";
    }

    //商品分页查询:
    @GetMapping("/list")
    public PageDTO<Item> queryByPage(@RequestParam Integer page, @RequestParam Integer size) {
        Page<Item> result = itemService.page(new Page<>(page, size));
        return new PageDTO<>(result.getTotal(), result.getRecords());
    }

    //根据id查询商品:
    @GetMapping("{id}")
    public Item queryById(@PathVariable Long id) {
        return itemService.getById(id);
    }

    //新增商品:
    @PostMapping()
    public void addCommodity(@RequestBody Item item) {
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        item.setStatus(2);
        itemService.save(item);
    }

    //上架商品:
    @PutMapping("/status/{id}/{status}")
    public void ground(@PathVariable Long id, @PathVariable Integer status) {
        itemService.update().set("status", status).eq("id", id).update();
    }
    //修改商品
    @PutMapping()
    public void updateCommodity(@RequestBody Item item){
        item.setCreateTime(new Date());
        item.setStatus(null);
        itemService.updateById(item);
    }
    //根据id删除商品
    @DeleteMapping("{id}")
    public void deleteById(@PathVariable Integer id){
        itemService.removeById(id);
    }



}
